package com.simpleaccount.Search;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.simpleaccount.Db.DbUtils;
import com.simpleaccount.R;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 超 on 2017/7/14.
 */

public class SearchActivity extends FragmentActivity{

    private incomeFragment_search fmincome_search;
    private OutcomeFragment_search fmpay_search;
    private FragmentManager fManager;
    @Bind(R.id.tvback_search)

    ImageView tvback_search;
    @Bind(R.id.tv_title)
    TextView tvrcjz_search;
    @Bind(R.id.tvzc_search)
    TextView tvzc_search;
    @Bind(R.id.tvsr_search)
    TextView tvsr_search;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        ButterKnife.bind(this);
        initView();
        clickMenu(0);
        DbUtils.init(this);
    }
    @Override
    protected void onStart() {
        super.onStart();
    }
    @Override
    protected void onResume() {
        super.onResume();
        if(null!=fmincome_search)
            fmincome_search.refresh();
        if(null!=fmpay_search)
            fmpay_search.refresh();
    }
    public void clickMenu(int vID) {
        FragmentTransaction trans = fManager.beginTransaction();
        hideFragment(trans);
        setFragment(vID, trans);
        trans.commit();
        if(vID==0) {
            tvsr_search.setTextColor(getResources().getColor(R.color.灰色));
            tvzc_search.setTextColor(Color.parseColor("#426ab3"));
        }
        if(vID==1){
            tvzc_search.setTextColor(getResources().getColor(R.color.灰色));
            tvsr_search.setTextColor(Color.parseColor("#426ab3"));
        }
    }
    private void initView() {
        fManager = getSupportFragmentManager();

    }
    public void hideFragment(FragmentTransaction trans) {
        if (null != fmincome_search) {
            trans.hide(fmincome_search);
        }
        if (null != fmpay_search) {
            trans.hide(fmpay_search);
        }
    }

    private void setFragment(int vid, FragmentTransaction trans) {
        switch (vid) {
            case 0:
                if (null == fmpay_search) {
                    fmpay_search = new OutcomeFragment_search();
                    trans.add(R.id.fragment_accounting_search, fmpay_search);
                } else {
                    trans.show(fmpay_search);

                }
                break;
            case 1:
                if (null == fmincome_search) {
                    fmincome_search = new incomeFragment_search();
                    trans.add(R.id.fragment_accounting_search, fmincome_search);
                } else {
                    trans.show(fmincome_search);
                }
                break;

            default:
                break;
        }
    }
    @OnClick({R.id.tvzc_search, R.id.tvsr_search,R.id.tvback_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvzc_search:
                clickMenu(0);
                break;
            case R.id.tvsr_search:
                clickMenu(1);
                break;
            case R.id.tvback_search:
                finish();
                break;
        }
    }
    @Override
    public void onActivityResult(int requestCode,int resultCode,Intent intent){
        if(requestCode==0&&resultCode==0){

            Toast.makeText(this,"已经更新了，亲！",Toast.LENGTH_LONG).show();

        }

    }
}
